package com.cw.jeeyt.controller.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cw.jeeyt.api.dto.response.JsonResultDto;
import com.cw.jeeyt.common.DataTable;
import com.cw.jeeyt.common.ResultData;
import com.cw.jeeyt.common.enums.EnumOperateFlag;
import com.cw.jeeyt.common.web.BaseController;
import com.cw.jeeyt.service.bo.system.SystemCityBo;
import com.cw.jeeyt.service.directive.PageDictDirective;
import com.cw.jeeyt.service.directive.PermissionDirective;
import com.cw.jeeyt.service.intf.system.SystemCityService;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * 城市信息控制类
 *
 * @author chenw
 * @version 1.0.0
 */
@Controller
@RequestMapping("/systemCity")
public class SystemCityController extends BaseController {
    @Resource
    private SystemCityService systemCityService;

    /**
     * 跳转到列表查询页面
     *
     * @param systemCityBo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/query")
    public ModelAndView query(SystemCityBo systemCityBo, String countryId) throws Exception {
        ModelAndView mv = new ModelAndView("system/systemCity_index");
        mv.addObject("provinceId", systemCityBo.getProvinceId());
        mv.addObject("countryId", countryId);
        mv.addObject("permission", new PermissionDirective());
        return mv;
    }

    /**
     * 加载DataTable列表数据
     *
     * @param dataTable 请求数据
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/listSystemCity")
    public ResultData listSystemCity(@RequestBody DataTable dataTable, String provinceId) throws Exception {

        SystemCityBo systemCityBo = JSONObject.parseObject(JSON.toJSONString(dataTable.getQueryCond()),
            SystemCityBo.class);
        if (systemCityBo == null) {
            systemCityBo = new SystemCityBo();
        }
        systemCityBo.setProvinceId(provinceId);
        PageInfo<SystemCityBo> pageInfo = systemCityService.queryByPage(systemCityBo, dataTable.getStart(),
            dataTable.getLength());
        return ResultData.builder()
            .draw(dataTable.getDraw())
            .data(CollectionUtil.isNotEmpty(pageInfo.getList())?pageInfo.getList():new ArrayList())
            .recordsTotal(pageInfo.getTotal())
            .recordsFiltered(pageInfo.getTotal())
            .build();
    }

    /**
     * 跳转到表单页面
     *
     * @param id 主键
     * @param operation 操作  add 新增 edit修改 view查看
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/to-form")
    public ModelAndView toForm(String id, String operation) throws Exception {
        ModelAndView mv = new ModelAndView("system/systemCity_form");
        SystemCityBo systemCityBo = null;
        if (EnumOperateFlag.ADD.getCode()
            .equals(operation)) {
            systemCityBo = new SystemCityBo();
        } else {
            systemCityBo = systemCityService.getSystemCityById(id);
        }
        mv.addObject("systemCity", systemCityBo);
        mv.addObject("operation", operation);
        mv.addObject("PageDict", new PageDictDirective());
        return mv;
    }

    /**
     * 根据操作类型保存数据
     *
     * @param systemCityBo
     * @param operation add新增  edit修改 view查看
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/saveSystemCity", method = RequestMethod.POST)
    public JsonResultDto saveSystemCity(SystemCityBo systemCityBo, String operation) {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            if (operation.equals(EnumOperateFlag.ADD.getCode())) {
                systemCityService.insertSystemCity(systemCityBo);
            } else if (operation.equals(EnumOperateFlag.EDIT.getCode())) {
                systemCityService.updateSystemCity(systemCityBo);
            }
            jsonResultDto.setMsg("保存成功");
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResultDto.setMsg("保存失败:" + e.getMessage());

        }
        jsonResultDto.setOperation(operation);
        return jsonResultDto;
    }

    /**
     * 删除城市信息
     *
     * @param systemCityBo
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public JsonResultDto deleteSystemCity(SystemCityBo systemCityBo) {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            systemCityService.deleteSystemCity(systemCityBo);
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResultDto.setMsg("删除失败：" + e.getMessage());
        }
        return jsonResultDto;
    }

    /**
     * 根据省份id加载城市信息
     *
     * @param provinceId
     */
    @RequestMapping(value = "/load_city")
    @ResponseBody
    public JsonResultDto loadSystemCityByProvinceId(String provinceId) {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            List<SystemCityBo> systemCityList = systemCityService.loadSystemCityByProvinceId(provinceId);
            Map<String, Object> map = new HashMap<>();
            map.put("systemCityList", systemCityList);
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();

            jsonResultDto.setMsg("加载城市信息失败：" + e.getMessage());
        }
        return jsonResultDto;
    }
}
